﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.Core.Controls;

namespace SalesManagement.Controls.List
{
    public partial class CategoryForm : System.Web.UI.UserControl
    {
        //public event EventHandler SaveClick;
        public event FormEventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {


                SaveClick(this, new FormEventArgs("phóng"));
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string categoryId)
        {
            CategoryEntity categoryE = new CategoryEntity();
            CategoryService categoryS = CategoryService.GetInstance();
            categoryE.CategoryId = int.Parse(categoryId);
            categoryE = categoryS.LoadByPrimaryKey(categoryE);
            if (null == categoryE)
                return;
            hdfCategoryId.Value = categoryE.CategoryId;
            txtCategoryName.Text = categoryE.CategoryName;
            txtDescription.Text = categoryE.Description;
            ckbIsActive.Checked = categoryE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {

            CategoryEntity categoryE = new CategoryEntity();
            CategoryService categoryS = CategoryService.GetInstance();
            if ("" != hdfCategoryId.Value.ToString())
            {
                string categoryId = hdfCategoryId.Value.ToString();
                categoryE.CategoryId = int.Parse(categoryId);
                categoryE = categoryS.LoadByPrimaryKey(categoryE);
                categoryE.CategoryName = txtCategoryName.Text;
                categoryE.Description = txtDescription.Text;
                categoryE.IsActive = ckbIsActive.Checked;
                categoryE.UpdatedDate = DateTime.Now;
                categoryE.UpdatedBy = 1;
                categoryS.Update(categoryE);
            }
            else
            {
                categoryE.CategoryName = txtCategoryName.Text;
                categoryE.Description = txtDescription.Text;
                categoryE.IsActive = ckbIsActive.Checked;
                categoryE.CreatedDate = DateTime.Now;
                categoryE.CreatedBy = 1;
                categoryE.UpdatedDate = DateTime.Now;
                categoryE.UpdatedBy = 1;
                categoryS.Insert(categoryE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}